const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");



// 预收商品列表
router.post('/bill/getYsgList', async ctx => {
    let transType = 153403
    let buId = ctx.request.body.buId
    let tenantId = ctx.user.tenantId
    let sql = `
    select a.billDate,a.billNo,a.id,a.id relId,b.id ysId,a.transTypeName,c.name buName,a.buId,
         b.invId,b.invName,b.spec,b.calNum,u.name unit,b.qty,b.price,b.amount,
         b.amount-b.hxAmount leftAmount,b.qty-hx.qty leftQty,b.remark
         from invoice a inner join invoice_info_ys b on a.id=b.iid and a.tenantId=${tenantId} 
         and a.buId=${buId} and a.billState='accounted' and a.transType=${transType}
             left join unit u on b.unitId=u.id 
             left join ( 
             SELECT tenantId,ysId,sum(qty) qty,sum(amount) amount   from (
              SELECT a.tenantId,isnull(b.ysId,0) ysId,b.qty,b.amount from invoice a inner join  v_main_info b on a.tenantId=b.tenantId and a.id=b.iid where a.tenantId=${tenantId} and a.transType=150601 and a.billState='accounted'
             ) a WHERE ysId<>0 GROUP BY tenantId,ysId
         ) hx on a.tenantId=hx.tenantId and b.id=hx.ysId
         left join contact c on a.buId=c.id and a.tenantId=c.tenantId 
    where b.qty-hx.qty > 0 
`
    let ret = await db.query(sql, {type: QueryTypes.SELECT})
    return ctx.suc('', ret )

})
module.exports = router;